package com.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bean.Material;
import com.service.MaterialService;

/**
 * @author author : 张鹏
 * @date createTime：2018年1月11日 下午9:51:26
 * @Description: 该类的功能描述
 */
@Controller
@RequestMapping("material")
public class MaterialController {

	@Autowired
	private MaterialService materialService;

	Material material;

	/**
	 * 查询所有原料
	 * 
	 * @return
	 */
	@RequestMapping("getAllMaterial")
	public String getAllMaterial(HttpServletRequest request) {
		request.setAttribute("materialList", materialService.getAllMaterial());
		return "materialManage";
	}
	
	
	@RequestMapping("getAllMaterialAjax")
	@ResponseBody
	public List<Material> getAllMaterialAjax(HttpServletRequest request) {
		return materialService.getAllMaterial();
	}
	
	@RequestMapping("getMaterialByPrimaryKeyAJAX")
	@ResponseBody
	public Material getMaterialByPrimaryKey(int materialId) {
		return materialService.getMaterialByPrimaryKey(materialId);
	}
	
	@RequestMapping("getMaterialByMaterialIdOrMaterialNameAJAX")
	@ResponseBody
	public List<Material> getMaterialByMaterialIdOrMaterialNameAJAX(String searchWords) {
		return materialService.getMaterialByMaterialIdOrMaterialName(searchWords);
	}
	
	
	@RequestMapping("getMaterialByPrimaryKey")
	public String getMaterialByPrimaryKey(int materialId,HttpServletRequest request) {
		material=materialService.getMaterialByPrimaryKey(materialId);
		request.setAttribute("material", material);
		return "editMaterial";
	}
	
	@RequestMapping("getMaterialByPrimaryKeyReturnTo")
	public String getMaterialByPrimaryKeyReturnTo(int materialId,String returnTo,HttpServletRequest request) {
		material=materialService.getMaterialByPrimaryKey(materialId);
		request.setAttribute("material", material);
		return returnTo;
	}
	

	/**
	 * 创建新的原料
	 * 
	 * @param MaterialName
	 * @return
	 */
	@RequestMapping("createNewMaterial")
	@ResponseBody
	public String createNewMaterial(String materialName, int unitId,Double materialPrice) {
		material = new Material();
		
		material.setMaterialName(materialName);
		material.setUnitId(unitId);
		material.setMaterialPrice(materialPrice);
		return materialService.createNewMaterial(material) + "";
	}

	/**
	 * 编辑原料
	 * 
	 * @param MaterialId
	 * @param MaterialName
	 * @return
	 */
	@RequestMapping("editMaterial")
	@ResponseBody
	public String editMaterial(int materialId, String materialName, int unitId,Double materialPrice) {
		material = new Material();
		material.setMaterialId(materialId);
		material.setMaterialName(materialName);
		material.setUnitId(unitId);
		material.setMaterialPrice(materialPrice);
		return materialService.editMaterial(material) + "";
	}

	/**
	 * 删除原料
	 * 
	 * @param MaterialId
	 * @return
	 */
	@RequestMapping("deleteMaterial")
	@ResponseBody
	public String deleteMaterial(int materialId) {
		return materialService.deleteMaterial(materialId) + "";
	}

}
